package com.shoesmail.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
 * @author ：CDB
 * @description：发送邮件工具类
 * @date ：2025/9/17 9:53
 */
@Slf4j
@Component
public class EmailSendUtil {
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Value("${mail.header}")
    private String header;



    public boolean sendMail(String to,String text){
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(fromEmail);
            helper.setTo(to);
            helper.setSubject(header);
            helper.setText(text);
            javaMailSender.send(helper.getMimeMessage());
            return true;
        }catch (javax.mail.MessagingException e) {
            e.printStackTrace();
            log.info("发送邮件失败,要发送到{}邮箱",to);
            throw new RuntimeException(e);
        }

    }
}
